/**
 * @author v.lugovsky
 * created on 16.12.2015
 */
(function () {
    'use strict';

    angular.module('BlurAdmin.pages.class')
        .controller('ClassAddManageCtrl', ClassAddManageCtrl);

    /** @ngInject */
    function ClassAddManageCtrl($scope,ClassModel,id,AppUtils,toastr,$window,$uibModalInstance,$uibModal) {
        var vm = this;

        //$scope.addStudent = function(memberId,index){
        //    ClassModel.getStudentClass({id: memberId}).$promise.then(function(response){
        //        var result = response.studentClass;
        //        vm.classNe = _.clone(result);
        //        var obj = {};
        //        if(AppUtils.isUndefinedOrNull(vm.classNe)){
        //            obj.id = memberId;
        //            obj.classId = id;
        //            $scope.insertStudent(obj,index);
        //        }else{
        //            var addStudentMode = AppUtils.confirm("该学生已被添加进其他班级，继续操作将会移除并添加进此班级，是否确认添加?");
        //            addStudentMode.result.then(
        //                function (selectedItem){
        //                    if(selectedItem == "yes"){
        //                        obj.memberClassId = vm.classNe.memberClassId;
        //                        obj.classId = id;
        //                        $scope.insertStudent(obj,index);
        //                    }
        //                }
        //            );
        //        }
        //    });
        //}
        //$scope.insertStudent = function (obj,index){
        //    if(!AppUtils.isUndefinedOrNull(obj)){
        //        ClassModel.insertStudentClass(obj).$promise.then(function(response){
        //            if(response.stat == "success"){
        //                toastr.success('添入成功！');
        //                vm.page.pageResult.splice(index,1);
        //            }else if(response.stat == "err"){
        //                AppUtils.alert("添入失败！");
        //            }
        //        });
        //    }
        //}

        $scope.addStudent = function(memberId,index){
            ClassModel.insertStudentClass({memberId: memberId, classId: id}).$promise.then(function(response){
                if(response.stat == "success"){
                    toastr.success('添入成功！');
                    vm.page.pageResult.splice(index,1);
                }else if(response.stat == "err"){
                    AppUtils.alert(response.msg);
                }
            });
        }





        vm.studentName = "";
        vm.page = {};
        vm.queryPage = function(){
            ClassModel.queryStudent({
                memberName: vm.studentName,
                memberType: "3",
                pageInfo: {
                    pageSize: 10,
                    pageNumber: vm.page.pageIndex
                }
            }).$promise.then(function(response){
                var result = response.pageResult;
                vm.page = _.clone(result);
            });
        }
        vm.queryPage();
    }

})();
